<template>
  <div>
    <Title />
    <el-container>
      <el-main>
        <el-card class="card" shadow="always">
          <el-form ref="retrievePwdRef" :model="retrievePwd" :rules="retrievePwdRules">
            <h3>忘记密码</h3>
            <el-form-item prop="phone">
              <el-input v-model="retrievePwd.phone" placeholder="请输入手机号" />
            </el-form-item>
            <el-form-item prop="validateCode">
              <el-input v-model="retrievePwd.validateCode" placeholder="请输入验证码"><template slot="append">
                <el-button @click="getValidateCode">获取验证码</el-button></template></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="success" class="btn-login" @click="submit">找回密码</el-button>
            </el-form-item>
            <p style="font-size:12px;">记起密码？<router-link to="/login" style="color:#3CB371;">立即登录</router-link></p>
          </el-form>
        </el-card>
      </el-main>
    </el-container>
    <el-footer />
  </div>
</template>

<script>
import Title from '@/components/title.vue'
import '@/static/css/login.css'
import {getValidateCode,retrievePwd} from '@/api/user'

export default {
  name: 'ForgetPassword',
  components: { Title },
  data() {
    return {
      retrievePwd: {
        phone: '',
        validateCode: ''
      },
      retrievePwdRules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '手机号不合法', trigger: 'blur' }
        ],
        validateCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 6, max: 6, message: '请输入有效的验证码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取验证码
    async getValidateCode() {
      const res = await getValidateCode()
      console.log(res)
    },
    // 提交
    submit() {
      this.$refs.retrievePwdRef.validate(async valid => {
        if (!valid) return
        const res = await retrievePwd(this.retrievePwd.phone,this.retrievePwd.validateCode)
        if (res.code === 200) {
          this.$router.push({
            name: 'SetNewPwd',
            params: {
              phone: res.data.phone
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped>
.card{
    margin:0 auto;
    width:20%;
    padding:20px 20px 10px 20px;
}
</style>
